/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.d.ui.view.verinor;

import com.d.lib.common.widget.TitleLayout;
import com.d.ui.view.ResourceTable;
import com.d.ui.view.verinor.adapter.VerAdapter;
import com.d.ui.view.verinor.models.VerInorModel;
import com.d.ui.view.verinor.models.VerModel;
import java.util.ArrayList;
import java.util.List;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;

public class VerInorAbilitySlice extends AbilitySlice implements Component.ClickedListener {

  private ListContainer mPullList;

  @Override
  public void onClick(Component component) {
    terminate();
  }

  @Override
  protected void onStart(Intent intent) {
    super.onStart(intent);
    super.setUIContent(ResourceTable.Layout_ability_verinor);
    bindView();
    initConfig();
  }

  private void bindView() {
    mPullList = (ListContainer) findComponentById(ResourceTable.Id_pull_list);

    TitleLayout titleLayout = (TitleLayout) findComponentById(ResourceTable.Id_tl_title);
    titleLayout.setBackClickListener(this::onClick);
  }

  private void initConfig() {
    final VerAdapter adapter = new VerAdapter(getContext(), getVerDataSet());
    mPullList.setItemProvider(adapter);
  }

  private List<VerModel> getVerDataSet() {
    List<VerModel> dataSet = new ArrayList<>();
    VerModel model;
    for (int i = 0; i < 30; i++) {
      switch (i) {
        case 1:
        case 3:
        case 4:
        case 10:
        case 12:
        case 22:
          model = new VerModel(1);
          model.dataSet = getVerInorDataSet(i);
          break;

        default:
          model = new VerModel(0);
          model.content = "" + i;
          break;
      }
      dataSet.add(model);
    }
    return dataSet;
  }

  private List<VerInorModel> getVerInorDataSet(final int position) {
    List<VerInorModel> models = new ArrayList<>();
    for (int i = 0; i < 7 * position; i++) {
      VerInorModel m = new VerInorModel();
      m.content = "" + position + "_" + i;
      models.add(m);
    }
    return models;
  }

  @Override
  protected void onForeground(Intent intent) {
    super.onForeground(intent);
  }
}
